import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Login1',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login.vue')
    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('../views/Register.vue')
    },
    {
        path: '/main',
        name: 'Main',
        component: () => import('../views/Main.vue'),
        meta: { requiresAuth: true },
        children: [
            {
                path: 'data',
                name: 'Data',
                component: () => import('../views/Data.vue'),
                meta: { requiresAuth: true }
            },
            {
                path: 'home',
                name: 'HomeView',
                component: () => import('../views/HomeView.vue'),
                meta: { requiresAuth: true }
            },
            {
                //CourseAttendanceRanking.vue
                path: 'courseAttendanceRanking',
                name: 'CourseAttendanceRanking',
                component: () => import('../views/CourseAttendanceRanking.vue'),
                meta: { requiresAuth: true } // 确保其他子路由也需要认证
            },
            {//AbsencePrediction
                path: 'absencePrediction',
                name: 'AbsencePrediction',
                component: () => import('../views/AbsencePrediction.vue'),
                meta: { requiresAuth: true } // 确保其他子路由也需要认证
            },
            {//AI Learning Analysis - 新增的AI学情分析路由
                path: 'aiLearningAnalysis', // 路由路径
                name: 'AILearningAnalysis', // 路由名称
                component: () => import('../views/AILearningAnalysis.vue'), // 对应的组件文件
                meta: { requiresAuth: true } // 通常主布局内的页面都需要认证
            },
            {//User Recommendation
                path: 'userRecommendation',
                name: 'UserRecommendation',
                component: () => import('../views/UserRecommendation.vue'),
                meta: { requiresAuth: true } // 确保其他子路由也需要认证
            },{
                //AttendanceSearch
                path: 'attendanceSearch',
                name: 'AttendanceSearch',
                component: () => import('../views/AttendanceSearch.vue'),
                meta: { requiresAuth: true } // 确保其他子路由也需要认证
            }

        ]
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
// 全局前置守卫
router.beforeEach((to, from, next) => {
    console.log('路由拦截：', to.path);

    // 判断该路由是否需要登录权限
    if (to.matched.some(record => record.meta.requiresAuth)) {
        console.log(`路由 ${to.path} 需要认证`);

        // 获取用户登录状态
        const userStr = localStorage.getItem('user');
        const user = userStr ? JSON.parse(userStr) : null;
        // 判断用户是否存在且有 token 属性
        const isLoggedIn = !!(user && user.token);

        console.log('用户登录状态:', isLoggedIn);

        if (!isLoggedIn) {
            console.log(`用户未登录，重定向到登录页 (原目标: ${to.fullPath})`);
            // 如果没有登录，重定向到登录页面
            next({
                path: '/login',
                // 保存我们所在的位置，以便以后再来
                query: { redirect: to.fullPath }
            });
            // 阻止当前导航
            return false; // 明确返回 false 或不调用 next()
        } else {
            // 如果已登录，则正常进入
            console.log('用户已登录，允许访问');
            next();
        }
    } else {
        // 不需要登录权限的页面，直接进入
        console.log(`路由 ${to.path} 无需认证，直接访问`);
        next();
    }
});
export default router